<?php
/**
 * 商品
 *
 * @copyright  Copyright (c) 2015-2025 NcPHP Inc. (http://www.ncphp.com)
 * @license    http://www.ncphp.com/
 * @link       http://www.ncphp.com/
 * @author     NcPHP Team
 * @since      File available since Release v1.1
 */
defined('InNcPHP') or exit('Access Invalid!');

class ticketControl extends BaseHomeControl{

    public function __construct(){
        if($_GET['op']=='collect' || $_GET['op']=='place_info' || $_GET['op']=='sel_info' || $_GET['op']=='update_info'){
            $this->_must_login = true;
        }
        parent::__construct();
    }

    /**
     * 景区列表
     */
    public function  indexOp()
    {
        /* @var ticketModel $ticket_model  */
        $ticket_model = Model('ticket');
        $area_model  = Model('area');
        $category_model = Model('ticket_category');

        //获取所有门票列表
        if($_SESSION['location']){
            $location = $_SESSION['location'];
            $field = '*';
            $distance = 'SQRT(pow('. $location['lng'] .' - tk_lat, 2) + pow('. $location['lat'] .'  - tk_lng, 2)) AS distance';
            $field .= ','.$distance;
        }

        //var_dump($_SESSION['location']);
        $condition = array();
        $condition['tk_status'] = 1;
        if($cate_id = $_GET['cate_id']){
            $condition['tk_cate1'] = intval($cate_id);//分类ID
        }
        if($area_id = $_GET['area']){
            $condition['tk_area_id'] = $area_id; //地区ID
        }
        if($keyword = trim($_GET['keyword'])){
            $condition['tk_title'] = array('like','%'.$keyword.'%');
        }
        if($order=$_GET['order']){
            //排序
            $order_arr = explode('|',$order);
            $order = $order_arr[0].' '.$order_arr[1];
        }else{
            if($_SESSION['location']){
                $order = 'distance asc';
            }
        }
        $ticket_list = $ticket_model->getTicketListPage($condition,$field,5,$order);
        if($ticket_list && is_array($ticket_list)){
            foreach($ticket_list as $key => $value){
               $ticket_list[$key]['tk_thumb'] = UPLOAD . 'product/' .$value['tk_thumb'];
                if($value['distance']){
                    $distance = $value['distance'];
                    $distance = round($distance * 10000) / 100;
                    if($distance<0.1){
                        $ticket_list[$key]['distance']='小于100m';
                    }else{
                        $ticket_list[$key]['distance'] = $distance."km"; // KM
                    }
                }
                $ticket_list[$key]['area_name'] = $area_model->getfby_id($value['tk_area_id']);
            }
        }
        Tpl::output('ticket_list',$ticket_list);
        //AJAX加载分页
        if(isset($_GET['isajax']) && $_GET['isajax'] == 1){
            Tpl::showpage('ticket.index.page','null_layout');
            exit;
        }
        $cityID  = $ticket_model->getTicketAreaId();//地区列表
        if($cityID){
            $area_arr = array();
            foreach($cityID as $item =>$value){
                $cityID[$item]['name'] = $area_model->getfby_id($value['tk_area_id']);
            }
            Tpl::output('area_arr',$cityID);
        }
        //门票底下所有分类
        $cate = $category_model->getCategoryList(array('tc_pid'=>TICKET_MARK,'is_del'=>0));
        Tpl::output('TotalNum',pagecmd("gettotalnum"));
        Tpl::output('TotalPage',pagecmd("gettotalpage"));
        Tpl::output('cate',$cate);
        Tpl::showpage('ticket.index','new_layout');
    }

    /**
     * 景点详情页面
     */
    public function ticket_infoOp()
    {
        $tk_id = intval($_GET['tk_id']);
        /* @var ticketModel $ticket_model*/
        $ticket_model = Model('ticket');
        $ticket_info = $ticket_model->getTicketInfo(array('tk_id'=>$tk_id,'tk_status'=>1));
        /* @var specModel $spec_model*/
        $spec_model = Model('spec');
        $condition = array();
        $condition['type'] = TICKET_MARK;
        $condition['is_show'] = 1;
        $condition['goods_id'] = $tk_id;
        //获取底下所有的票券类型
        $filed = 'spec_id,surface_img,market_price,price,label,spec_name';
        $spec_list = $spec_model->getSpecListPage($condition,$filed,50);
        if($spec_list && is_array($spec_list)){
            foreach($spec_list as $k =>$v){
                $spec_list[$k]['surface_img'] = getGoodsImg($v['surface_img'],TICKET_MARK);
            }
        }
        if(!empty($ticket_info['audio'])){
            $ticket_info['audio'] = UPLOAD.'video/'.$ticket_info['audio'];
        }
        $ticket_info['tk_thumb'] = UPLOAD . 'product/' .$ticket_info['tk_thumb'];
        /* @var ticket_imagesModel $images_model */
        $images_model = Model('ticket_images');
        //景点相册
        $images = $images_model->getTicketImageList(array('tk_id'=>$tk_id,'img_deleted'=>0,'type'=>TICKET_MARK));
        if($images && is_array($images)){
            foreach($images  as $image =>$vo){
                $images[$image]['img_path'] = UPLOAD . 'product/' .$vo['img_path'];
            }
        }
        Tpl::output('spec_list',$spec_list);
        Tpl::output('images',$images);
        Tpl::output('ticket_info',$ticket_info);
        Tpl::showpage('ticket_info','new_layout');
    }

    /**
     * 门票详情页面
     */
    public function detailOp()
    {
        /* @var ticketModel $ticket_model */
        $ticket_model = Model('ticket');
        /** @var collectModel $collect_model */
        $collect_model = Model('collect');
        $tk_id = intval($_GET['tid']);
        $spec_id = intval($_GET['sid']);//票型ID
        /* @var specModel $spec_model */
        $spec_model = Model('spec');
        $condition = array();
        $condition['spec_id'] = $spec_id;
        $condition['is_show'] = 1;
        $condition['type'] = TICKET_MARK;
        $spec_info = $spec_model->getSpecInfo($condition);
        !$spec_info && showMessage('该商品已经下架','','html','error');
        $ticket = $ticket_model->getTicketInfo(array('tk_id'=>$spec_info['goods_id'], 'tk_status'=>1));
        //下架
        !$ticket && showMessage('该商品已经下架~', '', 'html', 'error');

        $ticket['tk_face'] = UPLOAD . 'product/' . $ticket['tk_face'];
        Tpl::output('spec_info',$spec_info);
        $date = array(
            'today' => date('Y-m-d'),
            'exist' => true
        );
        if ($ticket['tk_type'] == 1) {//设置每日价格的
            /* @var ticket_dateModel $date_model */
            $date_model = Model('ticket_date');
            $date['exist'] = $date_model->getTicketDateInfo(array('tk_id'=>$tk_id,'date'=>$date['today']));
        }
        //客服电话
        $site_phone = Model('setting')->getfby_name('site_phone','value');
        Tpl::output('site_phone', $site_phone);

        Tpl::output('date', $date);

        //查询是否收藏
        Tpl::output('collect', $collect_model->getCollectInfo(array(
            'ticket_id'=>$tk_id,
            'member_id'=>$_SESSION['member']['member_id'],
        )));
        //票型轮播图
        $img_model = Model('spec_images');
        $images = $img_model->getSpecImageList(array('spec_id'=>$spec_id,'img_deleted'=>0));
        if($images && is_array($images)){
            foreach($images as $image =>$vo){
                $images[$image]['img_path'] = getGoodsImg($vo['img_path'],TICKET_MARK);
            }
        }
        Tpl::output('images', $images);

        Tpl::output('ticket', $ticket);
        if (in_array($ticket['tk_cate2'], array(7,8,10))) {
            Tpl::showpage('ticket.detail.free');
        } else {
            Tpl::showpage('ticket.detail');
        }
    }

    /**
     * ajax收藏/取消收藏
     */
    public function collectOp()
    {
        /* @var ticketModel $ticket_model */
        $ticket_model = Model('ticket');
        /** @var collectModel $collect_model */
        $collect_model = Model('collect');

        $tk_id = intval($_POST['tk_id']);

        if ($_POST['type'] == 'un') {
            $res = $collect_model->delCollect(array(
                'member_id' => $_SESSION['member']['member_id'],
                'ticket_id' => $tk_id,
            ));
            $res && output_data(1);
        } else {
            $ticket = $ticket_model->getTicketInfo(array('tk_id'=>$tk_id), 'tk_cate1');
            if ($ticket) {
                $res = $collect_model->addCollect(array(
                    'member_id' => $_SESSION['member']['member_id'],
                    'ticket_id' => $tk_id,
                    'cate1' => $ticket['tk_cate1'],
                    'add_time' => time(),
                ));
                $res && output_data(1);
            }
        }
        output_data(0);
    }



    /**
     * 售价日历
     */
    public function calendarOp()
    {
        /* @var ticketModel $ticket_model */
        $ticket_model = Model('ticket');
        /* @var  specModel $spec_model*/
        $spec_model = Model('spec');
        $ticket_type_model = Model('ticket_type');
        $spec_id = intval($_GET['sid']);
        $spec_info = $spec_model->getSpecInfo(array('spec_id'=>$spec_id,'type'=>TICKET_MARK));
        $tk_id = intval($spec_info['goods_id']);
        $ticket = $ticket_model->getTicketInfo(array('tk_id'=>$spec_info['goods_id']));
        if ($ticket['tk_type'] == 0) {
            /* @var ticket_skuModel $sku_model */
            $sku_model = Model('ticket_sku');
            $cur_sku = $sku_model->getTicketSkuList(array('tk_id'=>$tk_id));
            //当前sku
            Tpl::output('cur_sku', $cur_sku);
        } elseif ($ticket['tk_type'] == 1) {
            /* @var ticket_dateModel $date_model */
            $date_model = Model('ticket_date');
            /* @var ticket_sku_dateModel $sku_model_date */
            $sku_model_date = Model('ticket_sku_date');
            $today = date('Y-m-d');
            $exist = $date_model->getTicketDateInfo(array('tk_id'=>$tk_id,'date'=>$today));
            if ($exist) {
                $cur_sku = $sku_model_date->getTicketSpecSkuList(array('date_id'=>$exist['date_id'],'ticket_sku_date.spec_id'=>$spec_id));
                if($cur_sku && is_array($cur_sku)){
                    foreach($cur_sku as $ke => $vo){
                        $type_name = $ticket_type_model->getTicketType($vo['type_id']);
                        $cur_sku[$ke]['type_name'] = $type_name['type_name'];
                    }
                    //输入当日sku,如果当前日期有设置的话
                    Tpl::output('cur_sku', $cur_sku);
                }

            }
            //获取固定售价日历
            $sku_model = Model('ticket_sku');
            $fixation_sku = $sku_model->getTicketSkuList(array('tk_id'=>$tk_id,'spec_id'=>$spec_id));
            //var_dump($fixation_sku);
            //当前sku
            Tpl::output('fixation_sku', $fixation_sku);
            //日历里的价格
            $date_price = array();
            $dates = $date_model->getTicketDateList(array('tk_id'=>$tk_id, 'date'=>array('egt', $today)));
            foreach ($dates as $date) {
                $price = $sku_model_date->getTicketSpecSku(array('date_id'=>$date['date_id'],'ticket_sku_date.spec_id'=>$spec_id));
                if($price){
                    $date_price[] = array(
                        'date' => $date['date'],
                        'price' => $price['sku_price']
                    );
                }

            }

            Tpl::output('date_price', json_encode($date_price));
        }

        Tpl::output('ticket', $ticket);

        Tpl::output('spec_info',$spec_info);
        Tpl::showpage('ticket.calendar');
    }

    /**
     * 改变日期ajax请求门票sku
     */
    public function change_dateOp()
    {
        /* @var ticket_dateModel $date_model */
        $date_model = Model('ticket_date');
        /* @var ticket_sku_dateModel $sku_model_date */
        $sku_model_date = Model('ticket_sku_date');

        $tk_id = intval($_POST['tid']);
        $sid   = intval($_POST['sid']);
        $date = $_POST['date'];
        $ticket_type_model = Model('ticket_type');
        $date_info = $date_model->getTicketDateInfo(array('tk_id'=>$tk_id,'date'=>$date));
        if ($date_info) {
            $skus = $sku_model_date->getTicketSpecSkuList(array('date_id'=>$date_info['date_id'],'ticket_sku_date.spec_id'=>$sid));
            if ($skus) {
                //说明存在
                foreach($skus as $key =>$vo){
                    $type_name = $ticket_type_model->getTicketType($vo['type_id']);
                    $skus[$key]['type_name'] = $type_name['type_name'];
                }
                Tpl::output('skus', $skus);
            }else{
                //不存在获取固定的售价日历
                /* @var ticket_skuModel $sku_model */
                $sku_model = Model('ticket_sku');
                $skus = $sku_model->getTicketSkuList(array('spec_id'=>$sid));
                //当前sku
                Tpl::output('skus', $skus);
            }
        }else{
            //获取固定的售价日历
            /* @var ticket_skuModel $sku_model */
            $sku_model = Model('ticket_sku');
            $skus = $sku_model->getTicketSkuList(array('spec_id'=>$sid));
            //当前sku
            Tpl::output('skus', $skus);
        }
        Tpl::showpage('ticket.changedate', 'null_layout');
    }

    /**
     * 下单填写信息
     */
    public function place_infoOp()
    {
        if (chksubmit()) {
            /* @var ticketModel $ticket_model */
            $ticket_model = Model('ticket');

            $tk_id = intval($_POST['tid']);
            $sid = intval($_POST['sid']);//规格ID
            $date = $_POST['date'];
            $ticket = $ticket_model->getTicketInfo(array('tk_id'=>$tk_id, 'tk_status'=>1));
            if ($ticket) {

                $skus = array();
                $totalAmount = 0;
                /* @var ticket_dateModel $date_model */
                $date_model = Model('ticket_date');
                $date_info = $date_model->getTicketDateInfo(array('tk_id'=>$tk_id,'date'=>$date));
                $sku_model = Model('ticket_sku_date');
                foreach ($_POST['sku'] as $sku_id=>$num) {
                 if (intval($num) > 0) {
                     $sku = $sku_model->getTicketSku(array('sku_id'=>$sku_id,'spec_id'=>$sid));
                            $sku['num'] = intval($num);
                            $totalAmount += floatval($sku['sku_price']) * intval($num);
                            $skus[] = $sku;
                      }
                    }


                Tpl::output('price_type',1);
                if(!isset($skus[0]['sku_id'])){
                    //说明用户选的是固定的价格
                    unset($skus);
                    $skus = array();
                    $sku_model = Model('ticket_sku');
                    foreach ($_POST['sku'] as $sk_id=>$num2) {
                        if (intval($num2) > 0) {
                            $sku = $sku_model->getTicketSku(array('sku_id'=>$sk_id));
                            $sku['num'] = intval($num2);
                            $totalAmount += floatval($sku['sku_price']) * intval($num2);
                            $skus[] = $sku;
                        }
                    }
                    Tpl::output('price_type',2);
                }
                Tpl::output('sid',$sid);
                Tpl::output('totalAmount', $totalAmount);
                Tpl::output('date', $_POST['date']);
                Tpl::output('tk_id', $tk_id);
                Tpl::output('skus', $skus);
                Tpl::showpage('ticket.placeinfo');
            }
        }
    }

    /**
     * 选择姓名电话等
     */
    public function sel_infoOp()
    {
        /* @var member_infoModel $member_info_model */
        $member_info_model = Model('member_info');
        $infos = $member_info_model->getMemberInfoList(array('member_id'=>$_SESSION['member']['member_id']));
        Tpl::output('infos', $infos);
        Tpl::showpage('ticket.selinfo', 'null_layout');
    }

    /**
     * 更新联系人信息
     */
    public function update_infoOp()
    {
        /* @var member_infoModel $member_info_model */
        $member_info_model = Model('member_info');
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $id = intval($_POST['id']);
            $params = array(
                'mb_name' => trim($_POST['name']),
                'mb_phone' => trim($_POST['phone']),
                'mb_idcard' => trim($_POST['idcard']),
            );
            $res = $member_info_model->updateMemberInfo(array('id'=>$id), $params);
            echo json_encode(array('res'=>$res));
        } else {
            $id = intval($_GET['id']);
            $info = $member_info_model->getMemberInfo(array('id'=>$id));
            Tpl::output('info', $info);
            Tpl::showpage('ticket.updateinfo', 'null_layout');
        }
    }
}
